/**********************************************
** @Des: This file ...
** @Author: xiaomeng79
** @Date:   17-11-17 上午9:57
***********************************************/
package controllers

import (
	"time"
	"github.com/xiaomeng79/go-rbac/models"
	"github.com/xiaomeng79/go-rbac/libs"
	"github.com/astaxie/beego"
)

type AdminController struct {
	BaseController
}

/**
添加
 */
func (this *AdminController) Add() {
	if this.isPost() {//post请求
		Admin := new(models.Admin)
		Admin.Username = libs.FilterStr(this.GetString("username"))
		Admin.Password,Admin.Salt = libs.Password(5,libs.FilterStr(this.GetString("password")))
		Admin.Sex,_ = this.GetUint8("sex")
		Admin.Created_at = time.Now().Unix()
		Admin.Updated_at = time.Now().Unix()
		Admin.Is_usable = 1

		role_ids := make([]int,0)
		this.Ctx.Input.Bind(&role_ids,"role_ids")

		if err := Admin.Add(role_ids);err !=nil {
			this.ajaxMsg(err.Error(),MSG_ERR)
		}
			this.ajaxMsg("添加成功",MSG_OK)

	} else {
		//角色列表
		Role := new(models.Role)
		Roledata := Role.GetRoleLst()
		this.Data["Title"] = "添加管理员"
		this.Data["Navurl"] = "lst"
		this.Data["Navname"] = "管理员列表"
		this.Data["Currentname"] = "添加管理员"
		this.Data["Roledata"] = Roledata
		this.display()
	}
}

/**
列表
 */
func (this *AdminController) Lst() {
	Admin := new(models.Admin)
	result := Admin.Lst()
	this.Data["Title"] = "管理员列表"
	this.Data["Navurl"] = "lst"
	this.Data["Navname"] = "管理员列表"
	this.Data["Currentname"] = "管理员列表"
	this.Data["Data"] = result
	this.display()
}

/**
修改
 */
func (this *AdminController) Edit() {
	if this.isPost() {//post请求
		Admin := new(models.Admin)
		Admin.Sex,_ = this.GetUint8("sex")
		Admin.Id,_ = this.GetInt("id")
		Admin.Updated_at = time.Now().Unix()

		role_ids := make([]int,0)
		this.Ctx.Input.Bind(&role_ids,"role_ids")

		if err := Admin.Edit(role_ids,"sex","updated_at");err !=nil {
			this.ajaxMsg(err.Error(),MSG_ERR)
		}
		this.ajaxMsg("修改成功",MSG_OK)

	} else {
		Admin_id,_ := this.GetInt("id")
		Admin,err := models.GetAdminById(Admin_id)
		if err != nil {
			this.redirect(this.URLFor("AdminController.Lst"))
		}
		//角色列表
		Role := new(models.Role)
		Roledata := Role.GetRoleLst()
		this.Data["Data"] = Admin
		this.Data["Title"] = "修改管理员"
		this.Data["Navurl"] = "lst"
		this.Data["Navname"] = "管理员列表"
		this.Data["Currentname"] = "修改管理员"
		this.Data["Roledata"] = Roledata
		this.display()
	}
}

/**
删除
 */
func (this *AdminController) Del() {
		Id, _ := this.GetInt("id")
		if Id == 1 {
			this.ajaxMsg("超级管理员不能删除", MSG_ERR)
		}
		Admin := new(models.Admin)
		//软删除
		if err := Admin.Del(Id); err != nil {
			this.ajaxMsg(err.Error(), MSG_ERR)
		}

		this.ajaxMsg("删除成功", MSG_OK)
}

/**
修改密码
 */
func (this *AdminController) EditPassword() {

	if this.isPost() {//post请求
		//Admin := new(models.Admin)
		//Admin.Username = libs.FilterStr(this.GetString("username"))
		//Admin.Password,Admin.Salt = libs.Password(5,libs.FilterStr(this.GetString("password")))
		//Admin.Sex,_ = this.GetUint8("sex")
		//Admin.Created_at = time.Now().Unix()
		//Admin.Updated_at = time.Now().Unix()
		//Admin.Is_usable = 1
		//
		//role_ids := make([]int,0)
		//this.Ctx.Input.Bind(&role_ids,"role_ids")
		//
		//if err := Admin.Add(role_ids);err !=nil {
		//	this.ajaxMsg(err.Error(),MSG_ERR)
		//}
		//this.ajaxMsg("添加成功",MSG_OK)

	} else {
		this.Data["Title"] = "修改密码"
		this.Data["Navurl"] = "lst"
		this.Data["Navname"] = "管理员列表"
		this.Data["Currentname"] = "修改密码"
		this.display()
	}
}

/**
登录
 */
func (this *AdminController) Login() {

	if this.isPost() {//post请求
		username := libs.FilterStr(this.GetString("username"))
		password := libs.FilterStr(this.GetString("password"))
		Admin := new(models.Admin)
		//软删除
		var err error
		res := make(map[string]interface{})
		if res, err = Admin.Login(username,password); err != nil {
			this.ajaxMsg(err.Error(), MSG_ERR)
		}
		//设置session
		this.SetSession("admin_id",libs.InterfaceToString(res["id"]))
		this.SetSession("admin_username",libs.InterfaceToString(res["username"]))
		this.ajaxMsg("登录成功",MSG_OK)
	} else {
		this.display()
	}
}

/**
退出
 */
func (this *AdminController) Logout() {
	this.DestroySession()
	this.redirect(beego.URLFor("AdminController.Login"))
}